package com.tianji.learning.job;

import com.tianji.common.utils.BeanUtils;
import com.tianji.common.utils.CollUtils;
import com.tianji.learning.constants.RedisConstants;
import com.tianji.learning.domain.po.PointsBoard;
import com.tianji.learning.domain.po.PointsBoardSeason;
import com.tianji.learning.domain.vo.PointsBoardItemVO;
import com.tianji.learning.service.IPointsBoardSeasonService;
import com.tianji.learning.service.IPointsBoardService;
import com.tianji.learning.utils.TableInfoContext;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liuchun
 * @version 1.0
 * @date 2024/9/14 11:23
 */
@Slf4j
@Component
public class PointsBoardPersistentJob {

    @Autowired
    private IPointsBoardSeasonService seasonService;
    @Autowired
    private IPointsBoardService boardService;
    @Autowired
    private IPointsBoardService pointsBoardService;

    //@Scheduled(cron = "0 07 15 * * ?")
    @XxlJob("createTableJob")
    public void createPointsBoardTableOfLastSeason(){
        log.debug("定时任务生成榜单表.....");
        //获取当前时间
        LocalDate now = LocalDate.now();
        LocalDate time = now.minusMonths(1);
        Integer season = seasonService.querySeasonByTime(time);
        if (season == null){
            return;
        }
        //创建数据库
        boardService.createPointsBoardTableBySeason(season);
    }

    //将上个赛季信息持久化到DB
    @XxlJob("savePointsBoard2DB")
    public void savePointsBoard2DB(){
        log.debug("定时任务持久化榜单信息.....");
        //获取上个月时间
        LocalDate now = LocalDate.now();
        LocalDate time = now.minusMonths(1);
        //查询Redis中上个赛季数据
        String format = time.format(DateTimeFormatter.ofPattern("yyyyMM"));
        String key = RedisConstants.POINTS_BOARD_KEY_PREFIX + format;
        log.info("key:{}",key);
        Integer pageNo = 1;
        Integer pageSize = 1000;
        //动态计算表名
        Integer id = seasonService.querySeasonByTime(time);
        TableInfoContext.setInfo("points_board_" + id);
        List<PointsBoard> boardList = new ArrayList<>();
        log.debug("开始保存到数据库.......");
        log.debug("测试");
        //持久化到DB
        while (true){
            List<PointsBoardItemVO> pointsBoardItemVOS = pointsBoardService.queryCurrentRankByRedis(key, pageNo, pageSize);
            log.info("第{}页数据，{}",pageNo,pointsBoardItemVOS);
            //校验数据是否存在
            if(CollUtils.isEmpty(pointsBoardItemVOS)){
                break;
            }
            for (PointsBoardItemVO vo : pointsBoardItemVOS) {
                PointsBoard board = BeanUtils.copyBean(vo, PointsBoard.class);
                board.setId(vo.getRank().longValue());
                board.setRank(null);
                boardList.add(board);
            }
            //log.debug("开始保存到数据库.......");
            pointsBoardService.saveBatch(boardList);
            pageNo++;
        }
        //清空
        TableInfoContext.remove();
    }
}
